package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.OrderQuery;
import com.coffee.order.api.sku.result.OrderResult;
import com.coffee.order.api.sku.service.OrderService;
import com.coffee.order.service.sku.bean.Order;
import com.coffee.order.service.sku.dao.OrderDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class OrderServiceImpl implements OrderService {
	@Resource
	private OrderDao	orderDao;

	@Override
	public Page<OrderResult> getPager(OrderQuery param, PagerInfo pr) {
		Page<OrderResult> pager=new Page<OrderResult>(pr.getStart(), pr.getPageSize());
		Integer count=orderDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<Order> list=orderDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, OrderResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(OrderQuery param){
		Integer count=orderDao.count(param);
		return count;
	}
	
	@Override
	public OrderResult getDetail(Integer id){
		Order item=orderDao.findById(id);
		if(item==null){
			return null;
		}
		OrderResult result=new OrderResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<OrderResult> getList(OrderQuery param, PagerInfo pr){
		List<Order> list=orderDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, OrderResult.class);
	}
	
	@Override
	public List<OrderResult> getList(OrderQuery param){
		List<Order> list=orderDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, OrderResult.class);
	}
	
	@Override
	public List<OrderResult> getList(){
		OrderQuery param = new OrderQuery();
		List<Order> list=orderDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, OrderResult.class);
	}


	@Override
	public void delete(Integer id) {
		orderDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(OrderResult result)throws BusinessException {
		Order item=new Order();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			orderDao.insert(item);
		}else {
			Order tmp=orderDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			orderDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,OrderResult> getItemMap(Set<Integer> idSet){
		Map<Integer, OrderResult> map=new HashMap<Integer, OrderResult>();
		List<Order> list=orderDao.findByIds(StringUtil.setToList(idSet));
		List<OrderResult> resultList=DTOUtils.convertList(list, OrderResult.class);
		for(OrderResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,OrderResult> getItemMap(){
		Map<Integer, OrderResult> map=new HashMap<Integer, OrderResult>();
		OrderQuery param=new OrderQuery();
		List<Order> list=orderDao.page(param, 0, Integer.MAX_VALUE);
		List<OrderResult> resultList=DTOUtils.convertList(list, OrderResult.class);
		for(OrderResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
